<?php
namespace app\common\service;

use app\common\model\Reply;
use think\Model;

class WxAutoReplyService extends BaseService
{

    /**
     * @param $sid
     * @param $p
     * @param $pageSize
     * @return array
     * 获取自动回复列表
     */
    public function getAutoReplyList($sid, $p, $pageSize){
        $where = [
            'a.sid' => $sid,
            'a.type' => ['<', 4],
            'a.status' => 1,
        ];
        $list_count = db()->name('reply')->alias('a')
                    ->join('res b', 'a.res_id = b.res_id', 'LEFT')
                    ->where($where)
                    ->count();

        $list = db()->name('reply')->alias('a')
                ->join('res b', 'a.res_id = b.res_id', 'LEFT')
                ->field('a.*,b.res,b.status as res_status')
                ->where($where)
                ->order('a.reply_id DESC')
                ->limit(($p-1)*$pageSize, $pageSize)
                ->select();
        $data = [];
        if($list){
            foreach ($list as $k => $v) {
                $data[$k]['reply_id'] = $v['reply_id'];
                $data[$k]['sid'] = $v['sid'];
                $data[$k]['keyword'] = $v['keyword'];
                $data[$k]['match_type'] = $v['match_type'];
                $data[$k]['type'] = $v['type'];
                $data[$k]['content'] = $v['type'] == 1 ? $v['content'] : json_decode($v['res'],true);
                $data[$k]['res_status'] = $v['res_status'];
            }
        }
        return ['content' => $data,'page' =>['count' =>$list_count]];
    }

    /**
     * 获取自动回复详情
     * @param $sid
     * @param $reply_id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getWxAutoReplyDetail($sid, $reply_id){
        $filed = 'reply_id,type,keyword,match_type,res_id,content';
        $detail = db('reply')->field($filed)->where(['reply_id' => $reply_id, 'sid' => $sid, 'status' => 1])->find();
        if(empty($detail)){
            return ['status' => 0, 'msg' => '该回复不存在'];
        }
        if($detail['type'] == 2 || $detail['type'] == 3){
            $res = db('res')->where(['res_id' => $detail['res_id'], 'status' => 1])->find();
            if($res){
                $temp = json_decode($res['res'],true);
                $temp['content'] = htmlspecialchars_decode($temp['content']);
                $detail['content'] = $temp;
            }
        }
        $data['content'] = $detail;
        return ['status' => 1, 'msg' => '获取成功', 'data' => $data];
    }


    /**
     * @param $sid
     * @param array $postParam
     * @return int|string
     * 添加微信自动回复
     */
    public function addWxAutoReply($sid, $postParam = []){
        $data = [
            'sid' => $sid,
            'keyword' => $postParam['keyword'],
            'match_type' => $postParam['match_type'],
            'type' => $postParam['type'],
            'content' => $postParam['type'] == 1 ? $postParam['content'] : '',
            'res_id' => $postParam['type'] == 1 ? 0 : $postParam['res_id']
        ];
        return db('reply')->insert($data);
    }

    /**
     * @param $sid
     * @param array $postParam
     * @return int|string
     * 更新微信自动回复
     */
    public function updateWxAutoReply($sid, $reply_id, $postParam = []){
        $data = [
            'keyword' => $postParam['keyword'],
            'match_type' => $postParam['match_type'],
            'type' => $postParam['type'],
            'content' => $postParam['type'] == 1 ? $postParam['content'] : '',
            'res_id' => $postParam['type'] == 1 ? 0 : $postParam['res_id']
        ];
        return db('reply')->where(['reply_id' => $reply_id, 'sid' => $sid])->update($data);
    }

    /**
     * @param $sid
     * @param array $postParam
     * @param $batch（1为批量删除）
     * @return int|string
     * 软删除微信自动回复
     */
    public function softDelete($sid, $reply_id, $batch = 0){
        $where = [];
        $where['sid'] = $sid;
        $where['reply_id'] = $batch == 1 ? ['in', $reply_id] : $reply_id;

        return db('reply')->where($where)->setField('status',-1);
    }


    /**
     * 根据关键词获取自动回复详情
     * @param $sid
     * @param $reply_id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getWxAutoReplyDetailByKeyword(Reply $replyModel,$sid,$keyword){

        $where_1 = [
            'keyword' => $keyword,
            'sid' => $sid,
            'match_type' => 1,
            'status' => 1
        ];
        $detail = $replyModel->where($where_1)->order('match_type asc')->find();
        if(empty($detail)){
            $where_2 = [
                'keyword' => ['like','%'.$keyword.'%'],
                'sid' => $sid,
                'match_type' => 2,
                'status' => 1
            ];
            $detail = $replyModel->where($where_2)->order('match_type asc')->find();
        }
        return $detail;
    }


    /**
     * 获取默认回复详情
     * @param $sid
     * @return array|false|\PDOStatement|string|Model
     */
    public function getReplyDefaultDetail($sid)
    {
        return db('reply_default')->where('sid', $sid)->find();
    }

    public function editReplyDefault($sid, $content)
    {
        return db('reply_default')->where('sid', $sid)->setField('content', $content);
    }
}







